home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / termutil.0 / termutil / termutils-2.0 / fgetline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-03  |  2.5 KB  |  91 lines

  1. /* fgetline -- read a line of an unlimited length.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #include <stdio.h>
  23.  
  24. #if STDC_HEADERS || HAVE_STRING_H
  25. #include <string.h>
  26. #else /* not STDC_HEADERS and not HAVE_STRING_H */
  27. #include <strings.h>
  28. #define strchr index
  29. #endif /* not STDC_HEADERS and not HAVE_STRING_H */
  30.  
  31. #ifdef STDC_HEADERS
  32. #include <stdlib.h>
  33. #else
  34. char *getenv ();
  35. char *malloc ();
  36. char *realloc ();
  37. #endif
  38.  
  39. /* Up to this many bytes are read with a call to
  40.    fgets.  Should be a bit larger than the expected
  41.    average line length */
  42. #define LINE_UNIT 64
  43.  
  44. /* Read a line of an unlimited length from fp
  45.    and return the pointer to it.  The caller is
  46.    responsible for freeing the buffer */
  47.  
  48. char *
  49. fgetline (fp)
  50.      FILE *fp;
  51. {
  52.   /* Buffer used to store the bytes we read */
  53.   char *readbuf;
  54.   /* Number of bytes we've already read so far */
  55.   int readcount;
  56.  
  57.   /* If we have already reached the end of the file,
  58.      do not bother allocating a buffer to call fgets */
  59.   if (feof (fp))
  60.     return 0;
  61.  
  62.   readcount = 0;
  63.   readbuf = malloc (LINE_UNIT);
  64.   if (readbuf == 0)
  65.     return 0;            /* What else can we do? */
  66.  
  67.   while (1)
  68.     {
  69.       if (fgets (readbuf + readcount, LINE_UNIT, fp) == 0)
  70.     {
  71.       if (readcount)
  72.         return readbuf;    /* We've read something */
  73.       else
  74.         return 0;        /* got EOF before reading anything */
  75.     }
  76.       if (strchr (readbuf + readcount, '\n') == 0)
  77.     {
  78.       /* fgets returned because the line was too long */
  79.       readcount += LINE_UNIT - 1;    /* fgets null-terminates the buffer */
  80.       readbuf = realloc (readbuf, readcount + LINE_UNIT);
  81.       if (readbuf == 0)
  82.         /* We could save the old readbuf and return it to the
  83.            caller, but some implementations of realloc
  84.            trashes the old buffer when it fails. */
  85.         return 0;
  86.     }
  87.       else
  88.     return readbuf;
  89.     }
  90. }
  91.